package ar.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import ar.Location.PlaceObject.PlaceCategoryManager;
import ar.Location.UI.DataFilterAdapter;
import ar.activity.R.string;

public class DataFilterActivity extends BaseActivity {

	public static Handler handlerUpdateGridView = null;
	GridView gvDataFilter = null;
	DataFilterAdapter dataFilterAdapter = null;
	OnItemClickListener onItemClick = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.grid_data_filter);
		super.onCreate(savedInstanceState);
		initEvent();
		initHandler();
		initAdapter();
		initView();
	}

	public void initEvent() {
		onItemClick = new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				PlaceCategoryManager.currentPlaceCategory = PlaceCategoryManager
						.categoryAtIndex(position);
				Intent intent = new Intent(DataFilterActivity.this,
						ListPlaceResultActivity.class);
				startActivity(intent);
			}
		};
	}
	
	public void initHandler() {
		// TODO Auto-generated method stub
		handlerUpdateGridView = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				synchronized (dataFilterAdapter) {
					if (dataFilterAdapter != null) {
						dataFilterAdapter.notifyDataSetChanged();
					}
				}
			}
		};
	}

	public void initAdapter() {
		dataFilterAdapter = new DataFilterAdapter(this);
	}

	public void initView() {
		tvWhatScreen.setText(res.getString(string.dataFilterScreenName));

		gvDataFilter = (GridView) findViewById(R.id.gridDataFilter);
		gvDataFilter.setOnItemClickListener(onItemClick);
		gvDataFilter.setAdapter(dataFilterAdapter);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
	
	@Override
	protected void onUpdateUI() {
		// TODO Auto-generated method stub
	//	setContentView(R.layout.grid_data_filter);
		initView();
		dataFilterAdapter.notifyDataSetChanged();
	}
}
